<template>
  <download-finish-top />
  <download-finish-content />
</template>

<script setup lang="ts">
import { onBeforeMount, onBeforeUnmount, onMounted, ref } from 'vue'
import useSetWindowDragSize from '@/hooks/use-set-window-drag-size'
import { invokeAsyncAPi, registerInformApi} from '@/utils/xz-fn'
import { useDownloadManageWindow } from '@/stores/download-manage-window'

import { storeToRefs } from 'pinia'

const downloadManageWindowStore = useDownloadManageWindow();
const { masterWindowId } = storeToRefs(downloadManageWindowStore)

const timeout = ref(0);//定时10s关闭页面

useSetWindowDragSize();

const getCurrentWindowId = async () => {//获取当前窗口id
  const id = await invokeAsyncAPi('jsMsg_GetWndID');
  downloadManageWindowStore.setDownloadFinishWindowId(id);
}

const getMasterWindowId = async () => {//获取主窗口id
  const data = await invokeAsyncAPi('jsMsg_GetWndInfo');
  if (data.payload) {
    downloadManageWindowStore.setMasterWindowId(data.payload);
  }
}

onBeforeUnmount(() => {
  if (timeout.value) {
    clearTimeout(timeout.value);
  }
})

const createCloseTimeout = () => {//创建关闭倒计时
  timeout.value = window.setTimeout(async () => {
    await invokeAsyncAPi('fq_NotifyWebMessageEx', masterWindowId.value, 'close-download-finish-window')
    invokeAsyncAPi('jsMsg_SetWndStatus', 0)
  }, 60 * 1000);
}

onMounted(() => {
  getCurrentWindowId();//获取主窗口id
  getMasterWindowId();//得到主窗口id
  createCloseTimeout();
})
</script>

<style scoped lang="scss">
</style>